﻿/***************************************************************
* Javascript Menu.js
* Author   : Jiang Xingbo
* Date     : 2011-3-13
* Required : 
*            Jskit.org/Base/*.*
***************************************************************/
var CellulaMenu = function(rHd){
    var __hd = (typeof(rHd)=="string")?rHd:"cellulaMenu";
    var __data = null;
    var __target = "_self";
    var __firstUrl = null;
    var __urlCount = 0;
    var __selectedItem = null;
    var __buildMenu = function(rData,rLayer){
        var _str = new Array();
        var _node = null;
        for(var i=0;i<rData.length;i++){
            _node = rData[i];
            _str.push('<div class="menu_layer'+(rLayer+1)+'">');
            if(_node.childNodes.length<=0){
                if(_node.img==""){
                    _str.push(unescape("%07"));
                }else{
                    _str.push('<img src="'+_node.img+'" width="16" height="16" border="0" />');
                }
            }
            if(_node.url=="" || _node.url=="#"){
                _str.push(_node.caption);
            }else{
                if(__firstUrl==null){
                    __firstUrl = _node.url;
                }
                _str.push('<a href="'+ _node.url +'"  onclick="return '+__hd+'.onClick(this,event);" id="menu_link_'+__urlCount+'"  target="'+__target+'">'+_node.caption+'</a></div>');
                __urlCount++;
            }
            _str.push('</div>');
            if(_node.childNodes.length>0){
                _str.push(__buildMenu(_node.childNodes,rLayer+1));
            }
        }
        _node = null;
        rData = null;
        rLayer = null;
        return _str.join('');
    };
    var __showMenu = function(){
        document.write(__buildMenu(__data,0));
    };
    this.onClick = function(sender,e){
        if(__selectedItem!=null){//first
            __selectedItem.style.color = "#0000ff";
        }
        sender.style.color = "#ff0000";
        __selectedItem = sender;
    };
    this.getFirstUrl = function(){
        if(__firstUrl==null){
            return "about:blank";
        }else{
            return __firstUrl;
        }
    };
    this.setTarget = function(v){
        __target = v;
    };
    this.load = function(v){
        __data = v;
    };
    this.display = function(rData){
        this.load(rData);
        __showMenu();
    };
};